package com.goingup.domain;

import java.util.ArrayList;

import com.goingup.domain.Vector.Direction;

public class Building {
	
	private ArrayList<Floor> floors = new ArrayList<Floor>();
	private ArrayList<Elevator> elevators = new ArrayList<Elevator>();
	RequestController controller;
	
	
	/**
	 * Create a building with the given number of floors and one elevator.
	 * @param numberOfFloors
	 */
	public Building(int numberOfFloors)
	{
		this(numberOfFloors, 1);
	}
	public Building(int numberOfFloors, int numberOfElevators) {
		controller = new RequestController();
		createFloors(numberOfFloors);
		createElevators(numberOfElevators);
		
	}
	private void createElevators(int numberOfElevators) {
		for (int i = 0; i < numberOfElevators; i++) {
			Elevator elevator = new Elevator(getGroundFloor());
			new Thread(elevator).start();
			elevators.add(i,elevator);
			controller.addRequestable(elevator);
		}
	}
	private void createFloors(int numberOfFloors) {
		for (int i = 0; i < numberOfFloors; i++) {
			Floor floor = new Floor(i+1, this);
			
			floors.add(i, floor);
			if(i==0)
				continue;
			floors.get(i-1).setAbove(floor);	
		}
	}
	public Floor getFloor(int floorNumber) {
		return floors.get(floorNumber-1); //zero indexed
	}
	public Floor getGroundFloor() {
		return getFloor(1);
	}
	/**
	 * Returns the requested elevator.  
	 * @param i
	 * @return
	 */
	public Elevator getElevator(int i) {
		return elevators.get(i-1); //zero indexed
	}
	public void requestPickupOn(Floor floor, Direction direction) {
		controller.requestPickupOn(floor, direction);
	}
	public int floorCount() {
		return this.floors.size();
	}
}
